/*
 * Copyright 2012 yura.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.opu.pcm.ui.client.actions;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.util.*;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import org.opu.pc.services.SystemSettingsService;
import org.opu.pc.services.SystemSettingsService.DefaultAuthenticationType;
import static org.opu.pc.services.SystemSettingsService.DefaultAuthenticationType.*;
import org.opu.pc.ui.client.actions.AbstractAction;
import org.opu.pc.ui.client.widgets.PropertiesEditPane;
import org.opu.pc.ui.client.widgets.PropertiesEditPane.PropertyOption;
import org.opu.pcm.services.beans.UserBean;
import org.opu.pcm.services.beans.UserBean.SecurityGroup;

/**
 *
 * @author yura
 */
public class SystemSettings extends AddUserUsersSettings{

    private SystemSettingsService systemSettingsService;
    private UserBean user;
    private PropertiesEditPane settingsProp;
    
    public SystemSettings(String acionLabels) {
        super(acionLabels);
    }

    public void setSystemSettingsService(SystemSettingsService systemSettingsService) {
        this.systemSettingsService = systemSettingsService;
    }

    @Override
    public String getName() {
        return "SystemSettings";
    }

    @Override
    protected void addPropertiesPane(JPanel cp, PropertiesEditPane pane) {
        JTabbedPane tp = new JTabbedPane();
        
        Map<String, Object> properties = new LinkedHashMap<String, Object>();
        properties.put("systemSettings.GuestAvailable", systemSettingsService.isGuestAvailable());
        properties.put("systemSettings.DefaultAuthenticationType", systemSettingsService.getDefaultAuthenticationType());
        
        Map<String, List<PropertiesEditPane.PropertyOption>> map 
                = new HashMap<String, List<PropertiesEditPane.PropertyOption>>();
        
        map.put("systemSettings.GuestAvailable", Arrays.asList(
				new PropertyOption("GuestAvailable.true", "Yes", "true"), 
				new PropertyOption("GuestAvailable.false", "No", "false")));
        
        map.put("systemSettings.DefaultAuthenticationType", Arrays.asList(
				getDATOption(SystemUserCredentials), 
				getDATOption(GuestCredentials), 
				getDATOption(NoCredentials)));
        
        settingsProp = new PropertiesEditPane(properties, i18n, map);
        tp.addTab(i18n.getSafe("systemSettings.settings"), settingsProp);
        
        
        JPanel t1 = new JPanel(new BorderLayout());
        super.addPropertiesPane(t1, pane);
        tp.addTab(i18n.getSafe("systemSettings.defaultUser"), t1);
        
        cp.add(tp);
    }

	@Override
	protected void initProp(LinkedHashMap<String, Object> properties) {
        user = userService.getGuestAccount();
		properties.put("user.name", user.getName());
//		properties.put("user.email", user.getEmail());
	}

    @Override
    protected List<SecurityGroup> getPrivilegesList() {
        return user.getSecurityGroups();
    }

    @Override
    protected AbstractAction getCancelAction(OkAction okAction) {
        AbstractAction cancelAction = super.getCancelAction(okAction);
        cancelAction.putValue(LARGE_ICON_KEY, null);
        return cancelAction;
    }
    
    @Override
    protected OkAction getOkAction(JDialog dialog, PropertiesEditPane pane) {
        OkAction ok = new OkAction(pane, dialog) {

            @Override
            public void runAction(ActionEvent e) throws Exception {
                hideDialog();
                
                Map<String, Object> prop = pane.getProperties();
				user.setSecurityGroups(list);
                user.setName((String)prop.get("user.name"));
                userService.updateUser(user);
                user = null;
                
                Map<String, Object> properties = settingsProp.getProperties();
                
                systemSettingsService.setGuestAvailable(Boolean.parseBoolean(
                        (String)properties.get("systemSettings.GuestAvailable")));
                
                systemSettingsService.setDefaultAuthenticationType(Enum.valueOf(
                        DefaultAuthenticationType.class,
                        (String)properties.get("systemSettings.DefaultAuthenticationType")));
                settingsProp = null;
            }
        };
        ok.putValue(LARGE_ICON_KEY, null);
        return ok;
    }

    @Override
    protected int getMinimalDialogWidth() {
        return 600;
    }
    
    private PropertyOption getDATOption(DefaultAuthenticationType type){
        return new PropertyOption("systemSettings.DefaultAuthenticationType." 
                + type.name(), type.name(), type.name());
    }
    
}
